package com.killer.linear;

/**
 *
 * @param <T>
 */
public class MyLinkedQueue<T> implements MyQueue<T> {

    /**
     * 头结点
     */
    private Node<T> head;

    /**
     * 元素个数
     */
    private int size=0;

    public void offer(T t) {
        size++;
        Node<T> node = new Node<T>(t);
        //如果头结点为空,则新插入的节点为头结点
        if (head == null) {
            head = node;
            return;
        }
        Node<T> tmp=head;
        //循环找到最后一个元素
        while (tmp.next != null) {
            tmp=tmp.next;
        }
        //插入到最后一个节点
        tmp.next=node;
    }

    public T poll() {
        if (size == 0) {
            throw new RuntimeException("队列已经为空");
        }
        size--;
        //出队则从头结点开始出
        Node<T> tmp=head;
        head=head.next;
        return tmp.data;
    }

    public int size() {
        return size;
    }

    private class Node<T>{
        T data;
        Node<T> next;

        public Node(T data) {
            this.data = data;
        }
    }
}
